package io.leo.leoui.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import io.leo.leoui.listener.OnInputListener;
import io.leo.leoutils.StringUtils;

/**
 * 由于Android
 */
@SuppressLint("AppCompatCustomView")
public class FillBlankView extends TextView {
    private OnInputListener listener;
    private Context context;

//    private final static String CHINESE_BLANK="　";
private final static String CHINESE_BLANK="_";
    class Blank{
        int startPos;
        int endPos;

        public Blank(int startPos, int endPos) {
            this.startPos = startPos;
            this.endPos = endPos;
        }
    }


    private SpannableStringBuilder spannableString;
    private List<Blank> blanks;

    private List<BlankClickableSpan> clickableSpans;

    public FillBlankView(Context context) {
        this(context,null);
    }

    public FillBlankView(Context context,AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        setTypeface(Typeface.createFromAsset(context.getAssets(),"firacode.ttf"));
        setMovementMethod(LinkMovementMethod.getInstance());
    }

    public void setOnInputListener(OnInputListener listener){
        this.listener = listener;
    }

    public void setQuestionText(String question,int...answerLength){
        blanks = new ArrayList<>();
        clickableSpans = new ArrayList<>();

        StringBuffer buf = new StringBuffer(question);
        int index;
        int i=0;
        while ((index=buf.indexOf("()"))!=-1){
            buf.replace(index,index+2, StringUtils.repeat(CHINESE_BLANK,answerLength[i]));
            blanks.add(new Blank(index,index+answerLength[i++]));
        }

        spannableString = new SpannableStringBuilder(buf.toString());
        for (i=0;i<blanks.size();i++){
            Blank blank = blanks.get(i);
            spannableString.setSpan(new UnderlineSpan(), blank.startPos, blank.endPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#4DB6AC")),blank.startPos,blank.endPos,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            BlankClickableSpan clickableSpan = new BlankClickableSpan(i,answerLength[i]);
            clickableSpans.add(clickableSpan);
            spannableString.setSpan(clickableSpan, blank.startPos, blank.endPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        setText(spannableString);
    }

    public String[] getAnswers(){
        String[] answers = new String[blanks.size()];
        for (int i=0;i<answers.length;i++){
            answers[i]=spannableString.subSequence(blanks.get(i).startPos,blanks.get(i).endPos).toString();
        }
        return answers;
    }


    public void performClick(int pos){
        if (pos>=clickableSpans.size())throw new RuntimeException("Pos index out of bounds ,max is "+(clickableSpans.size()-1)+" your input is "+pos);
        clickableSpans.get(pos).onClick(this);

    }

    public int getAnswerCount(){
        return clickableSpans.size();
    }
    public void setAnswer(int pos,String answer){
        Blank blank = blanks.get(pos);
        int len = blank.endPos-blank.startPos;
        if (answer.length()<len){
            answer += StringUtils.repeat(CHINESE_BLANK,len-answer.length());
        }else if (answer.length()>len){
            answer = answer.substring(0,len);
        }
        spannableString.replace(blank.startPos,blank.endPos,answer);
        setText(spannableString);
    }

    class BlankClickableSpan extends ClickableSpan {
        private int pos;
        private int len;
        public BlankClickableSpan(int pos,int len){
            this.len = len;
            this.pos=pos;
        }
        @Override
        public void onClick(View widget) {
            if (listener!=null){
                String olderString = spannableString.subSequence(blanks.get(pos).startPos,blanks.get(pos).startPos+len).toString();
                listener.onAcceptInput(pos,len,olderString.equals(StringUtils.repeat(CHINESE_BLANK,len))?"":olderString);
            }
        }
    }
}
